from django.db import models
from django.utils import timezone
from django.urls import reverse

class Post(models.Model):
    question = models.CharField(max_length=127, verbose_name="问题")
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    date_posted = models.DateTimeField(default=timezone.now)
    locate_id = models.IntegerField(
        verbose_name="定位码(可选)：任意输入一串数字，之后可根据数字定位到问题", null=True, blank=True
    )
    mrc_answered = models.BooleanField(verbose_name='是否进行过阅读理解',default=False)
    ordering = ['-date_posted']

    def __str__(self):
        return self.question
    
    def get_absolute_url(self):
        return reverse('home')

  
class Answer(models.Model):
    author = models.ForeignKey('auth.User', on_delete=models.CASCADE)
    question = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='answers')
    context = models.TextField(verbose_name="回答")
    date_answered = models.DateTimeField(default=timezone.now)

    def __str__(self):
        return f"{self.author} 在 {self.question} 中发表 {self.context}"


class MRCAnswer(models.Model):
    question = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='mrc_answers')
    context = models.TextField(verbose_name="回答")
    date_answered = models.DateTimeField(default=timezone.now)
    start = models.IntegerField(default=0)
    end = models.IntegerField(default=-1)

    def __str__(self):
        return f"{self.question} 中 {self.context}"